package business.shoppingcartsubsystem;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import middleware.exceptions.DatabaseException;
import business.exceptions.BackendException;
import business.exceptions.BusinessException;
import business.exceptions.RuleException;
import business.externalinterfaces.Address;
import business.externalinterfaces.CartItem;
import business.externalinterfaces.CreditCard;
import business.externalinterfaces.CustomerProfile;
import business.externalinterfaces.Rules;
import business.externalinterfaces.ShoppingCart;
import business.externalinterfaces.ShoppingCartSubsystem;

public class ShoppingCartSubsystemFacade implements ShoppingCartSubsystem {
	ShoppingCartImpl liveCart = new ShoppingCartImpl(new LinkedList<CartItem>());
	ShoppingCartImpl savedCart;
	Integer shopCartId;
	CustomerProfile customerProfile;
	Logger log = Logger.getLogger(this.getClass().getPackage().getName());

	// interface methods
	public void setCustomerProfile(CustomerProfile customerProfile) {
		this.customerProfile = customerProfile;
	}
	
	private ShoppingCart readTopLevelCartData(Integer cartId) {
		return null;
	
	}
	
	private List<CartItem> readCartItems(Integer cartId) {
		return null;
	}

	public void retrieveSavedCart() throws BackendException {
		try {
			DbClassShoppingCart dbClass = new DbClassShoppingCart();
			ShoppingCartImpl cartFound = dbClass.retrieveSavedCart(customerProfile);
			if(cartFound == null) {
				savedCart = new ShoppingCartImpl(new ArrayList<CartItem>());
			} else {
				savedCart = cartFound;
			}
		} catch(DatabaseException e) {
			throw new BackendException(e);
		}

	}
	public void runShoppingCartRules() throws RuleException, BusinessException {
		throw new RuleException("ShoppingCart rules have not been implemented.");
	}
	
	public void runFinalOrderRules() throws RuleException, BusinessException {
		throw new RuleException("Final Order rules have not been implemented.");
	}



	// make it a singleton
	private static ShoppingCartSubsystemFacade instance;

	public static ShoppingCartSubsystem getInstance() {
		if (instance == null) {
			instance = new ShoppingCartSubsystemFacade();
		}
		return instance;
	}

	/**
	 * Private constructor to make the facade a singleton
	 * 
	 */
	private ShoppingCartSubsystemFacade() {

	}

	public void addCartItem(String itemName, String quantity,
			String totalPrice, Integer pos) throws BackendException {
		// if a saved cart has been retrieved, it will be the live cart, unless
		// user has already added items to a new cart
		CartItemImpl item = new CartItemImpl(itemName, quantity, totalPrice);
		if (pos == null)
			liveCart.addItem(item);
		else
			liveCart.insertItem(pos, item);
	}

	public boolean deleteCartItem(int pos) {
		//implement
		return false;
	}

	public boolean deleteCartItem(String itemName) {
		return liveCart.deleteCartItem(itemName);
	}

	public List<CartItem> getLiveCartItems() {

		return liveCart.getCartItems();

	}

	public void setShippingAddress(Address addr) {
		liveCart.setShipAddress(addr);
	}

	public void setBillingAddress(Address addr) {
		liveCart.setBillAddress(addr);
	}

	public void setPaymentInfo(CreditCard cc) {
		liveCart.setPaymentInfo(cc);
	}

	public ShoppingCart getLiveCart() {
		return liveCart;
	}

	public void makeSavedCartLive() {
		liveCart = savedCart;
	}

	public void saveLiveCart() throws BackendException {
		//implement
	}


}
